﻿using System;
using Engine;
using Game.NetWork;
using GameEntitySystem;
using TemplatesDatabase;
using Test1;

namespace Game
{
    public class SubsystemNPCBehavior : SubsystemBlockBehavior, IUpdateable
    {
        public SubsystemParticles m_subsystemParticles;

        public Random m_random = new Random();

        private ContainerWidget m_screenLabelCanvasWidget;

        public ComponentPlayer m_componentPlayer;

        public SubsystemPlayers m_subsystemPlayers;

        private float m_screenLabelCloseTime;

        public bool firsttime = true;

        public bool firstSpawn;

        public bool firstSpawnYS;

        public SubsystemPlayers m_players;

        public bool isCityLordFirstburn;

        public bool isCityDodoFirstburn;

        public SubsystemBodies m_subsystemBodies;

        public bool isYSLiving;

        public override int[] HandledBlocks => new int[1];

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public override bool OnUse(Ray3 ray, ComponentMiner componentMiner)
        {
            IInventory inventory = componentMiner.Inventory;
            int activeBlockValue = componentMiner.ActiveBlockValue;
            if (Terrain.ExtractContents(activeBlockValue) == 0)
            {
                object obj = componentMiner.Raycast(ray, RaycastMode.Gathering);
                if (obj is BodyRaycastResult)
                {
                    ComponentMoney componentMoney = ((BodyRaycastResult)obj).ComponentBody.Entity.FindComponent<ComponentMoney>();
                    ComponentHealth componentHealth = ((BodyRaycastResult)obj).ComponentBody.Entity.FindComponent<ComponentHealth>();
                    ComponententityLord componententityLord = ((BodyRaycastResult)obj).ComponentBody.Entity.FindComponent<ComponententityLord>();
                    ComponententityDodo componententityDodo = ((BodyRaycastResult)obj).ComponentBody.Entity.FindComponent<ComponententityDodo>();
                    if (componentHealth.Health > 0f)
                    {
                        if (componentMoney != null && componententityLord != null)
                        {
                            componentMiner.Entity.FindComponent<ComponentTask>().Lordtask1 = true;
                            componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new LordButtonsPanelWidget(componentMiner, this, componentMoney, componentMiner.Inventory, componentMiner.ComponentPlayer, componentHealth);
                            return true;
                        }

                        if (componentMoney != null && componententityDodo != null)
                        {
                            componentMiner.Entity.FindComponent<ComponentTask>().Dodo1 = true;
                            componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new DodoButtonsPanelWidget(componentMiner, this, componentMoney, componentMiner.Inventory, componentMiner.ComponentPlayer, componentHealth);
                            return true;
                        }
                    }
                }
            }

            return false;
        }

        public void Update(float dt)
        {
            if (Time.PeriodicEvent(3.0, 0.0) && CommonLib.WorkType == WorkType.Server)
            {
                foreach (ComponentBody componentBody in m_subsystemBodies.m_componentBodies)
                {
                    ComponentYSChaseBehavior componentYSChaseBehavior = componentBody.Entity.FindComponent<ComponentYSChaseBehavior>();
                    if (componentYSChaseBehavior != null)
                    {
                        isYSLiving = true;
                        break;
                    }

                    isYSLiving = false;
                }

                CommonLib.Net.QueuePackage(new SubsystemNPCPackage(isYSLiving));
            }

            if (!Time.PeriodicEvent(2.0, 0.0) || CommonLib.WorkType != WorkType.Server || (!firstSpawn && !firstSpawnYS && !isCityLordFirstburn && !isCityDodoFirstburn))
            {
                return;
            }

            foreach (ComponentPlayer componentPlayer in m_players.ComponentPlayers)
            {
                if (componentPlayer == null)
                {
                    continue;
                }

                if (componentPlayer.Entity.FindComponent<ComponentTest1>().Areaname == "血泪之池" && firstSpawn)
                {
                    Point3 point = componentPlayer.Entity.FindComponent<ComponentTest1>().point1;
                    double num = Math.Sqrt(Math.Abs(1235 - point.X) * Math.Abs(1235 - point.X) + Math.Abs(1245 - point.Z) * Math.Abs(1245 - point.Z));
                    int num2 = (int)num;
                    int visibilityRange = SettingsManager.VisibilityRange;
                    if (num2 < 15 && firstSpawn)
                    {
                        Point3 p = (1233, 71, 1243);
                        Entity entity = DatabaseManager.CreateEntity(base.Project, "DoDo", throwIfNotFound: true);
                        ComponentFrame componentFrame = entity.FindComponent<ComponentFrame>();
                        ComponentSpawn componentSpawn = entity.FindComponent<ComponentSpawn>();
                        if (componentFrame != null && componentSpawn != null)
                        {
                            componentFrame.Position = new Vector3(p) + new Vector3(0.5f, 0f, 0.5f);
                            componentFrame.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitY, new Random().Float(0f, (float)Math.PI * 2f));
                            componentSpawn.SpawnDuration = 0f;
                            base.Project.AddEntity(entity);
                        }

                        firstSpawn = false;
                    }
                }
                else if (componentPlayer.Entity.FindComponent<ComponentTest1>().Areaname == "失落城市" && isCityDodoFirstburn)
                {
                    Point3 point2 = componentPlayer.Entity.FindComponent<ComponentTest1>().point1;
                    if (point2.X > 3072 && point2.X < 3622 && point2.Z > 3072 && point2.Z < 3622 && point2.Y > 64)
                    {
                        double num3 = Math.Sqrt(Math.Abs(3100 - point2.X) * Math.Abs(3100 - point2.X) + Math.Abs(3178 - point2.Z) * Math.Abs(3178 - point2.Z));
                        int num4 = (int)num3;
                        int visibilityRange2 = SettingsManager.VisibilityRange;
                        if (num4 < 15 && isCityDodoFirstburn)
                        {
                            Point3 p2 = (3100, 83, 3178);
                            Entity entity2 = DatabaseManager.CreateEntity(base.Project, "DoDo", throwIfNotFound: true);
                            ComponentFrame componentFrame2 = entity2.FindComponent<ComponentFrame>();
                            ComponentSpawn componentSpawn2 = entity2.FindComponent<ComponentSpawn>();
                            if (componentFrame2 != null && componentSpawn2 != null)
                            {
                                componentFrame2.Position = new Vector3(p2) + new Vector3(0.5f, 0f, 0.5f);
                                componentFrame2.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitY, new Random().Float(0f, (float)Math.PI * 2f));
                                componentSpawn2.SpawnDuration = 0f;
                                base.Project.AddEntity(entity2);
                            }

                            isCityDodoFirstburn = false;
                        }
                    }
                }

                if (componentPlayer.Entity.FindComponent<ComponentTest1>().Areaname == "失落城市" && firstSpawnYS)
                {
                    Point3 point3 = componentPlayer.Entity.FindComponent<ComponentTest1>().point1;
                    if (point3.X > 3425 && point3.X < 3517 && point3.Z > 3168 && point3.Z < 3273 && point3.Y > 64)
                    {
                        double num5 = Math.Sqrt(Math.Abs(3453 - point3.X) * Math.Abs(3453 - point3.X) + Math.Abs(3256 - point3.Z) * Math.Abs(3256 - point3.Z));
                        int num6 = (int)num5;
                        int visibilityRange3 = SettingsManager.VisibilityRange;
                        if (num6 < 5 && firstSpawnYS)
                        {
                            Point3 p3 = (3453, 83, 3256);
                            Entity entity3 = DatabaseManager.CreateEntity(base.Project, "YS", throwIfNotFound: true);
                            ComponentFrame componentFrame3 = entity3.FindComponent<ComponentFrame>();
                            ComponentSpawn componentSpawn3 = entity3.FindComponent<ComponentSpawn>();
                            if (componentFrame3 != null && componentSpawn3 != null)
                            {
                                componentFrame3.Position = new Vector3(p3) + new Vector3(0.5f, 0f, 0.5f);
                                componentFrame3.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitY, new Random().Float(0f, (float)Math.PI * 2f));
                                componentSpawn3.SpawnDuration = 0f;
                                base.Project.AddEntity(entity3);
                            }

                            firstSpawnYS = false;
                        }
                    }
                }

                if (!(componentPlayer.Entity.FindComponent<ComponentTest1>().Areaname == "失落城市") || !isCityLordFirstburn)
                {
                    continue;
                }

                Point3 point4 = componentPlayer.Entity.FindComponent<ComponentTest1>().point1;
                if (point4.X <= 3072 || point4.X >= 3622 || point4.Z <= 3072 || point4.Z >= 3622 || point4.Y <= 64)
                {
                    continue;
                }

                double num7 = Math.Sqrt(Math.Abs(3108 - point4.X) * Math.Abs(3108 - point4.X) + Math.Abs(3177 - point4.Z) * Math.Abs(3177 - point4.Z));
                int num8 = (int)num7;
                int visibilityRange4 = SettingsManager.VisibilityRange;
                if (num8 < 15 && isCityLordFirstburn)
                {
                    Point3 p4 = (3108, 71, 3177);
                    Entity entity4 = DatabaseManager.CreateEntity(base.Project, "BSLord", throwIfNotFound: true);
                    ComponentFrame componentFrame4 = entity4.FindComponent<ComponentFrame>();
                    ComponentSpawn componentSpawn4 = entity4.FindComponent<ComponentSpawn>();
                    if (componentFrame4 != null && componentSpawn4 != null)
                    {
                        componentFrame4.Position = new Vector3(p4) + new Vector3(0.5f, 0f, 0.5f);
                        componentFrame4.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitY, new Random().Float(0f, (float)Math.PI * 2f));
                        componentSpawn4.SpawnDuration = 0f;
                        base.Project.AddEntity(entity4);
                    }

                    isCityLordFirstburn = false;
                }
            }
        }

        public override void Save(ValuesDictionary valuesDictionary)
        {
            base.Save(valuesDictionary);
            valuesDictionary.SetValue("FS", firstSpawn);
            valuesDictionary.SetValue("FSYS", firstSpawnYS);
            valuesDictionary.SetValue("YSLife", isYSLiving);
            valuesDictionary.SetValue("CityLord", isCityLordFirstburn);
            valuesDictionary.SetValue("CityDodo", isCityDodoFirstburn);
        }

        public override void Load(ValuesDictionary valuesDictionary)
        {
            base.Load(valuesDictionary);
            m_subsystemParticles = base.Project.FindSubsystem<SubsystemParticles>(throwOnError: true);
            m_subsystemPlayers = base.Project.FindSubsystem<SubsystemPlayers>(throwOnError: true);
            m_subsystemBodies = base.Project.FindSubsystem<SubsystemBodies>(throwOnError: true);
            firsttime = valuesDictionary.GetValue("FT", defaultValue: true);
            firstSpawn = valuesDictionary.GetValue("FS", defaultValue: true);
            firstSpawnYS = valuesDictionary.GetValue("FSYS", defaultValue: true);
            isCityLordFirstburn = valuesDictionary.GetValue("CityLord", defaultValue: true);
            isCityDodoFirstburn = valuesDictionary.GetValue("CityDodo", defaultValue: true);
            isYSLiving = valuesDictionary.GetValue("YSLife", defaultValue: false);
            m_players = base.Project.FindSubsystem<SubsystemPlayers>(throwOnError: true);
        }

        public bool ErgodicBody(string target, Func<ComponentBody, bool> action)
        {
            target = target.ToLower();
            Vector2 vector = new Vector2(m_componentPlayer.ComponentBody.Position.X, m_componentPlayer.ComponentBody.Position.Z);
            DynamicArray<ComponentBody> dynamicArray = new DynamicArray<ComponentBody>();
            m_subsystemBodies.FindBodiesInArea(vector - new Vector2(64f), vector + new Vector2(64f), dynamicArray);
            foreach (ComponentBody item in dynamicArray)
            {
                switch (target)
                {
                    case "player":
                        {
                            ComponentPlayer componentPlayer4 = item.Entity.FindComponent<ComponentPlayer>();
                            if (componentPlayer4 != null && action(item))
                            {
                                return true;
                            }

                            break;
                        }
                    case "boat":
                        {
                            ComponentBoat componentBoat = item.Entity.FindComponent<ComponentBoat>();
                            if (componentBoat != null && action(item))
                            {
                                return true;
                            }

                            break;
                        }
                    case "creature":
                        {
                            ComponentPlayer componentPlayer = item.Entity.FindComponent<ComponentPlayer>();
                            ComponentCreature componentCreature = item.Entity.FindComponent<ComponentCreature>();
                            if (componentPlayer == null && componentCreature != null && action(item))
                            {
                                return true;
                            }

                            break;
                        }
                    case "vehicle":
                        {
                            ComponentDamage componentDamage3 = item.Entity.FindComponent<ComponentDamage>();
                            if (componentDamage3 != null && action(item))
                            {
                                return true;
                            }

                            break;
                        }
                    case "npc":
                        {
                            ComponentPlayer componentPlayer2 = item.Entity.FindComponent<ComponentPlayer>();
                            ComponentCreature componentCreature2 = item.Entity.FindComponent<ComponentCreature>();
                            ComponentDamage componentDamage = item.Entity.FindComponent<ComponentDamage>();
                            if (componentPlayer2 == null && (componentCreature2 != null || componentDamage != null) && action(item))
                            {
                                return true;
                            }

                            break;
                        }
                    case "all":
                        {
                            ComponentPlayer componentPlayer3 = item.Entity.FindComponent<ComponentPlayer>();
                            ComponentCreature componentCreature3 = item.Entity.FindComponent<ComponentCreature>();
                            ComponentDamage componentDamage2 = item.Entity.FindComponent<ComponentDamage>();
                            if ((componentPlayer3 != null || componentCreature3 != null || componentDamage2 != null) && action(item))
                            {
                                return true;
                            }

                            break;
                        }
                    default:
                        if (target == item.Entity.ValuesDictionary.DatabaseObject.Name.ToLower() && action(item))
                        {
                            return true;
                        }

                        break;
                }
            }

            return false;
        }
    }
}